package com.xm_jd.controller.goods;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xm_jd.model.dao.TianDao;
import com.xm_jd.model.entitys.car;
import com.xm_jd.model.entitys.tb_car;
import com.xm_jd.model.entitys.tb_shoop;
import com.xm_jd.model.entitys.tb_user;
import com.xm_jd.model.entitys.user;
import com.xm_jd.utils.BaseServlet;

/**查询购物车列表servlet
 * Servlet implementation class Goodscar
 */
@WebServlet("/addgoodscart")
public class Goodscar extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Goodscar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				TianDao dao=new TianDao();
				//��ȡ�����빺�ﳵʱ�û�ѡ�е�ֵ
				String shangid=request.getParameter("spid");
				String numa=request.getParameter("num");
				//��session��ȡ�û�
				user aa=(user) request.getSession().getAttribute("users");
				System.out.println(aa);
				//�ж��Ƿ�Ϊ��  Ϊ���ض��򵽵�¼����
				if(aa==null){
					this.forward("login");
				}else{
				
					car gw=new car();
					gw.setCar_user_id(aa.getUser_id());
					gw.setCar_shoop_sid(Integer.parseInt(shangid));
					gw.setCar_gounum(Integer.parseInt(numa));
					gw.setCar_xuanzhong("1");
					System.out.println(gw);
					//��ѯshangi
					List<tb_car> shsh=dao.shangs(Integer.parseInt(shangid));
					if(shsh.size()>0){
						dao.gidduibi(Integer.parseInt(shangid) , shsh.get(0).getCar_gounum() + 1);
						//response.sendRedirect("cartlist");
						this.redirect("/cartlist");
					}else{
						int i = dao.addgood(gw);
						if(i>0){
							//response.sendRedirect("cartlist");
							this.redirect("/cartlist");
						}else{
							//request.getRequestDispatcher("xq_body").forward(request, response);
							this.forward("xq_body");
						}
					}
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
